package com.ruoyi.kq.controller;

import java.util.List;
import java.util.UUID;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kq.domain.WorkRecord;
import com.ruoyi.kq.service.IWorkRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.alibaba.fastjson2.JSONObject;

/**
 * kqController
 *
 * @author huangdelu
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/kq/record")
@Slf4j
public class WorkRecordController extends BaseController
{
    @Autowired
    private IWorkRecordService workRecordService;

    /**
     * 查询kq列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WorkRecord workRecord)
    {
        startPage();
        List<WorkRecord> list = workRecordService.selectWorkRecordList(workRecord);
        return getDataTable(list);
    }

    /**
     * 导出kq列表
     */
    @Log(title = "kq", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkRecord workRecord)
    {
        List<WorkRecord> list = workRecordService.selectWorkRecordList(workRecord);
        ExcelUtil<WorkRecord> util = new ExcelUtil<WorkRecord>(WorkRecord.class);
        util.exportExcel(response, list, "kq数据");
    }

    /**
     * 获取kq详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(workRecordService.selectWorkRecordById(id));
    }

    /**
     * 新增kq
     */
    @Log(title = "kq", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkRecord workRecord)
    {
        return toAjax(workRecordService.insertWorkRecord(workRecord));
    }

    /**
     * 修改kq
     */
    @Log(title = "kq", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkRecord workRecord)
    {
        return toAjax(workRecordService.updateWorkRecord(workRecord));
    }

    /**
     * 删除kq
     */
    @Log(title = "kq", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(workRecordService.deleteWorkRecordByIds(ids));
    }

    /**
     * 获取打卡记录
     */
    @GetMapping("/getTime/{userId}")
    public AjaxResult getTime(@PathVariable("userId") Long userId, @RequestParam("date") String date) {
        // 参数校验
        if (userId == null) {
            return AjaxResult.error("用户ID不能为空");
        }
        if (StringUtils.isEmpty(date)) {
            return AjaxResult.error("日期不能为空");
        }

        try {
            // 查询指定用户指定日期的打卡记录
            List<WorkRecord> records = workRecordService.selectWorkRecordByUserIdAndDate(userId, date);
            return AjaxResult.success(records);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.error("获取打卡记录失败");
        }
    }

    /**
     * 提交打卡记录
     */
    @Log(title = "提交打卡记录", businessType = BusinessType.INSERT)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody JSONObject recordInfo)
    {
        try {
            // 构建打卡记录对象
            WorkRecord workRecord = new WorkRecord();
            // 生成UUID作为id
            workRecord.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            workRecord.setUserId(recordInfo.getString("userId"));
            workRecord.setName(recordInfo.getString("username"));
            workRecord.setDate(recordInfo.getString("today"));
            workRecord.setStartTime(recordInfo.getString("firstRecord"));
            workRecord.setEndTime(recordInfo.getString("endRecord"));
            // 设置创建时间
            workRecord.setCreateTime(new Date());

            // 保存打卡记录
            workRecordService.insertWorkRecord(workRecord);

            return success("打卡记录提交成功");
        } catch (Exception e) {
            logger.error("提交打卡记录失败", e);
            return error("打卡记录提交失败：" + e.getMessage());
        }
    }
}
